package Entities;

import digitshoplogic.DBAccessSingleton;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Discount_rule {
	public int ruleId;
	public int type;
	public BigDecimal percentage;
	public int other_count;
        public int count;   
        
    public Discount_rule() {
    }
    
  /*  public Discount_rule(int ruleId, int type, BigDecimal percentage, int other_count, int count) {
        this.ruleId = ruleId;
        this.type = type;
        this.percentage = percentage;
        this.other_count = other_count;
        this.count = count;
    }*/

     public static final Discount_rule getDiscountRuleByID(int rid) throws SQLException {
        Discount_rule rule = new Discount_rule();

        PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("select * from discount_rule where discount_rule.ruleId = ?");
         ps.setInt(1, rid);

        ResultSet rs = ps.executeQuery();

         if (rs.next()) {
            rule.ruleId = rs.getInt(1);
            rule.type = rs.getInt(2);
            rule.percentage = rs.getBigDecimal(3);
            rule.other_count = rs.getInt(4);
            rule.count = rs.getInt(5);
        }
        rs.close();
        con.close();
        return rule;
        }
     public static final void addDiscountRuleByObj(Discount_rule rule) throws SQLException {

         PreparedStatement ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("INSERT INTO discount_rule (`type`, percentage, other_count, `count`) "
                 + "VALUES (?,?,?,?)");

        // ps.setInt(1, rule.ruleId);
         ps.setInt(1, rule.type);
         ps.setBigDecimal(2, rule.percentage);
         ps.setInt(3, rule.other_count);
         ps.setInt(4, rule.count);

         ps.executeUpdate();
         ps.close();
         con.close();
        }

 public static final void deleteDiscountRuleByID(int rid) throws SQLException{

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("DELETE FROM discount_rule WHERE discount_rule.ruleId= ?");
	 ps.setInt(1, rid);
         ps.executeUpdate();
         ps.close();
         con.close();
     }
  public static final void updateDiscountRuleByObj(Discount_rule rule) throws SQLException{

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("UPDATE discount_rule SET "
                 + "discount_rule.type = ?,"
                 + "discount_rule.percentage = ?,"
                 + "discount_rule.other_count = ?,"
                 + "discount_rule.count = ? "
                 + "WHERE discount_rule.ruleId = ?");

	ps.setInt(1, rule.type);
        ps.setBigDecimal(2, rule.percentage);
        ps.setInt(3, rule.other_count);
        ps.setInt(4, rule.count);
        ps.setInt(5, rule.ruleId);

         ps.executeUpdate();
         ps.close();
         con.close();

     }


    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getOther_count() {
        return other_count;
    }

    public void setOther_count(int other_count) {
        this.other_count = other_count;
    }

    public BigDecimal getPercentage() {
        return percentage;
    }

    public void setPercentage(BigDecimal percentage) {
        this.percentage = percentage;
    }

    public int getRuleId() {
        return ruleId;
    }

    public void setRuleId(int ruleId) {
        this.ruleId = ruleId;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    } 

}